package com.iaz.tech.tools.tasks.invoker;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iaz.tech.tools.common.utils.DoublyLinkedList;
import com.iaz.tech.tools.property.IPropertyAble;
import com.iaz.tech.tools.tasks.ability.IScheduleAble;
import com.iaz.tech.tools.tasks.ability.ITaskItemAble;
import com.iaz.tech.tools.tasks.ability.ITaskPoolExecutorAble;
import com.iaz.tech.tools.tasks.pool.ZTecThreadsPool;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 用于执行的驻留
 */
@EqualsAndHashCode(callSuper=false)
@Data
public class NeuronTask extends Thread implements INeuronTaskAble, ITaskPoolExecutorAble, ITaskItemAble{
    /**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(NeuronTask.class);
    /* 
     * 同步
     */
	private static final String _SERIAL_SYN_TILE_FIX = "__SERIAL_NO_FIX";
    private static long _SERIALNO_FIX = 0L;
    /**
     * 任务ID
     */
    private long taskID = -1L;
    /**
     * 任务发送者
     */
    private ITaskItemAble sender = null;
    /**
     * 任务是否运行中
     */
    private boolean running = true;
    /**
     * 任务线程池
     */
    private ZTecThreadsPool pool = null;
    /**
     * 生成任务ID
     * @return
     */
    public static String generateCode(){
		synchronized( _SERIAL_SYN_TILE_FIX ){
			if( _SERIALNO_FIX > 9999){
				_SERIALNO_FIX = 0L;
			}
			_SERIALNO_FIX++;
		}
		return "TASK_FIX_" + _SERIALNO_FIX;
	}
    /**
     * 任务运行
     */
    @Override
    public void run() {
        while (this.isRunning()) {
            try {
                
                if ( this.sender != null){
                    this.sender.execute(this);
                }
            } catch (Exception e) {
                logger.error("NeuronTask run error, taskID: {}, sender: {}", this.taskID, this.sender, e);
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                logger.error("NeuronTask run error, taskID: {}, sender: {}", this.taskID, this.sender, e);
            }
        }
    }
    /**
     * 获取任务ID
     */
    @Override
    public long thisItemID() {
        return this.taskID;
    }
    /**
     * 任务执行
     */
    @Override
    public void execute(Object sender) {
         try {
            if ( this.sender != null){
                this.sender.execute(this);
            }
         } catch (Exception e) {
            logger.error("NeuronTask execute error, taskID: {}, sender: {}", this.taskID, this.sender, e);
         }
    }
    /**
     * 任务调用
     */
    @Override
    public void invoke(Object sender) {        
        throw new UnsupportedOperationException("Unimplemented method 'invoke'");
    }
    /**
     * 任务是否空闲
     */
    @Override
    public boolean isFree() {
        return this.sender == null;
    }
    /**
     * 任务注册
     */
    @Override
    public void registerTask(IScheduleAble ITaskItemAble) { 
        throw new UnsupportedOperationException("Unimplemented method 'registerTask'");
    }
    /**
     * 任务获取
     */
    @Override
    public IScheduleAble forTask() { 
        throw new UnsupportedOperationException("Unimplemented method 'forTask'");
    }
    /**
     * 任务释放
     */
    @Override
    public void free() {
        this.setRunning(false);
    }
    /**
     * 任务停止
     */
    @Override
    public void stopTask() {
        this.setRunning(false);
    }
    /**
     * 任务执行时间获取
     */
    @Override
    public long forExecuteTime() {
        long result = 0l;
        try {
            if (this.sender == null) {
                throw new IllegalArgumentException("NeuronTask forExecuteTime error, sender is null");
            }
            result = this.sender.forExecuteTime();
        } catch (Exception e) {
            logger.error("NeuronTask forExecuteTime error, taskID: {}, sender: {}", this.taskID, this.sender, e);
            result = 0l;
        }
        return result;
    }
}
